package com.flca.helper;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.flca.frw.context.FlcaCtx;
import com.flca.frw.dto.KeyValueDto;
import com.flca.frw.dto.KeyValuesDto;
import com.flca.frw.test.TestParameter;
import com.flca.frw.test.TestParamsMap;

public class StubModusHelper {

	private static List<String> sRegisteredServices = new ArrayList<String>();
	
	/**
	 * This adds a Class, that has a mock impl to a list.
	 * @param aClass
	 */
	public static void registerService(Class<?> aClass)
	{
		sRegisteredServices.add(aClass.getSimpleName());
	}
	
	public static List<String> getRegistedServices() {
		return sRegisteredServices;
	}
	
	public static KeyValuesDto getMockModiFromUser(FlcaCtx aCtx) 
	{
		KeyValuesDto result = new KeyValuesDto();

		for (String servicename : sRegisteredServices) {
			String key = makeKey(servicename);
			String value = TestParamsMap.INSTANCE.getValue(key);
			if (value == null) {
				value = "false";
			}
			KeyValueDto dto = new KeyValueDto();
			dto.setKey(servicename);
			dto.setValue(value);

			result.addKeyvalue(dto);
		}

		return result;
	}

	public static KeyValuesDto applyMockModiFromUser(KeyValuesDto args,
			FlcaCtx aCtx) 
	{
		Set<KeyValueDto> keyvalues = args.getKeyvalues();

		for (KeyValueDto keyValueDto : keyvalues) {
			String key = makeKey(keyValueDto.getKey());
			if ("true".equals(keyValueDto.getValue())) {
				TestParamsMap.INSTANCE.setParam(key, "true");
			} else {
				TestParamsMap.INSTANCE.setParam(key, "false");
			}
		}

		KeyValuesDto result = new KeyValuesDto();
		List<TestParameter> testparams = TestParamsMap.INSTANCE.getTestParameters();
		for (TestParameter tp : testparams) {
			KeyValueDto dto = new KeyValueDto(tp.getName(), tp.getValue());
			result.add(dto);
		}
		return result;
	}


	public static void applyModus(Class<?> aClass, boolean aFlag, FlcaCtx aCtx) 
	{
		String key = makeKey(aClass);
		if (aFlag) {
			TestParamsMap.INSTANCE.setParam(key, "true");
		} else {
			TestParamsMap.INSTANCE.setParam(key, "false");
		}
	}


	public static String makeKey(Class<?> aInterface)
	{
		return makeKey(aInterface.getSimpleName());
	}
	
	public static String makeKey(String aServicename)
	{
		return aServicename + ".mock";
	}
}
